package org.bibpessoal.entidades;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.PrePersist;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.sun.istack.Nullable;

@NamedQueries({
	@NamedQuery(
			name="Emprestimo.getLateLoans",
			query="SELECT * FROM Emprestimo l WHERE l.createdAt <= :maxDate && confirmedAt not null"),
	@NamedQuery(
			name="Emprestimo.getLoansForApprove",
			query="SELECT * FROM Emprestimo l WHERE l.confirmedAt is null"
		),
	@NamedQuery(
			name="Emprestimo.getLoansForReturn",
			query="SELECT * FROM Emprestimo l WHERE l.confirmedAt >= :maxDate && returnedAt is null "
		)
})

@Entity
public class Emprestimo implements Serializable {
	
	private static final long serialVersionUID = -7928000865630677480L;
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int id;
	private Date data;
	private Livro livro;
	private boolean ativo;
	
	@Nullable
	@OneToOne
	private Usuario requester = null;
	
	@OneToOne
	private Livro book;
	
	// Public Loan
	private String name;
	private String mail;
	private String fone;
	
	// Dates
	@Temporal(TemporalType.TIMESTAMP)
	private Date createdAt;
	@Temporal(TemporalType.TIMESTAMP)
	private Date confirmedAt;
	@Temporal(TemporalType.TIMESTAMP)
	private Date returnedAt;

	public Usuario getRequester() {
		return requester;
	}

	public void setRequester(Usuario requester) {
		this.requester = requester;
	}

	public Livro getBook() {
		return book;
	}

	public void setBook(Livro book) {
		this.book = book;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public String getFone() {
		return fone;
	}

	public void setFone(String fone) {
		this.fone = fone;
	}

	public Emprestimo() {
		super();
		ativo = true;
	}
	
	public Emprestimo(Usuario x, Livro y) {
		this.requester = x;
		this.book = y;
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}


	public Date getCreatedAt() {
		return createdAt;
	}
	
	@PrePersist
	public void setCreatedAt() {		
		this.createdAt = new Date();
	}

	public Date getConfirmedAt() {
		return confirmedAt;
	}

	public void setConfirmedAt() {
		this.confirmedAt = new Date();
	}

	public Date getReturnedAt() {
		return returnedAt;
	}

	public void setReturnedAt() {
		this.returnedAt = new Date();
	}
	
	// --

	@Temporal(TemporalType.DATE)
	public Date getData() {
		return data;
	}

	public void setData(Date data) {
		this.data = data;
	}
	
	// --

	@ManyToOne
	public Livro getLivro() {
		return livro;
	}

	public void setLivro(Livro livro) {
		this.livro = livro;
	}

	public boolean isAtivo() {
		return ativo;
	}

	public void setAtivo(boolean ativo) {
		this.ativo = ativo;
	}
	
	// -- 

}
